﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TileDemo.View
{
    class TextureAssets
    {
        public Texture2D m_tiles;

        public static int g_tileSize = 64;

        public static int MapTiles = 0;
        public static int CleanTiles = 192;
        public static int WeirdTiles = 320;


        public void LoadContent(ContentManager a_content)
        {
            m_tiles = a_content.Load<Texture2D>("tiles");
        }


        public static Rectangle GetTileSourceRectAndRotation(int a_x, int a_y, Model.Level a_level, out float a_rotation, int a_variation)
        {
            int mask = 0;

            bool[] isTileCoornerSet = { false, false, false, false };
            isTileCoornerSet[0] = a_level.IsTileSet(a_x + 1, a_y); //1
            isTileCoornerSet[1] = a_level.IsTileSet(a_x + 1, a_y + 1); //2
            isTileCoornerSet[2] = a_level.IsTileSet(a_x, a_y + 1);//4
            isTileCoornerSet[3] = a_level.IsTileSet(a_x, a_y);//8

            //Create a number from the four corners
            //gives numbers from 0 to 15 
            
            for (int i = 0; i < 4; i++)
            {
                if (isTileCoornerSet[i])
                {
                    mask |= (1 << i); //bit-shift i=0=>*0,i=1=>*2, i=2=>*4, i=3=>*8 
                }
            }
            int tileType =  (int)GetTileNumber(mask, out a_rotation);

            a_rotation = (float)a_rotation * (float)Math.PI / 2.0f;


            int variation = (a_x + a_y * 32) % 2 == 1 ? a_variation : a_variation + g_tileSize;
            Rectangle sourceUV = new Rectangle(tileType * g_tileSize, variation, g_tileSize, g_tileSize);

            return sourceUV;

        }

        enum Tile
        {
            None,
            Full,
            Corner,
            Side,
            Three,
            Opposite
        }

        private static Tile GetTileNumber(int a_mask, out float a_outRotation)
        {

            switch (a_mask)
            {
                case 0: a_outRotation = 0; return Tile.None;
                case 1: a_outRotation = 1; return Tile.Corner;
                case 2: a_outRotation = 2; return Tile.Corner;
                case 3: a_outRotation = 1; return Tile.Side;
                case 4: a_outRotation = 3; return Tile.Corner;
                case 5: a_outRotation = 0; return Tile.Opposite; //lower left and upper right
                case 6: a_outRotation = 2; return Tile.Side;
                case 7: a_outRotation = 2; return Tile.Three;
                case 8: a_outRotation = 0; return Tile.Corner; //only lower right
                case 9: a_outRotation = 0; return Tile.Side;
                case 10: a_outRotation = 3; return Tile.Opposite;
                case 11: a_outRotation = 1; return Tile.Three;
                case 12: a_outRotation = 3; return Tile.Side; //only lower right
                case 13: a_outRotation = 0; return Tile.Three;
                case 14: a_outRotation = 3; return Tile.Three;
                case 15: a_outRotation = 0; return Tile.Full; //only lower right
            }


            a_outRotation = 0;
            return 0;
        }
    }



}
